/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.leboncoin.web.servlet;

import com.leboncoin.bean.ClassifiedBean;
import com.leboncoin.bean.SearchItemBean;
import flexjson.JSONSerializer;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.persistence.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ben
 */
@WebServlet(name = "GraphServlet", urlPatterns = {"/graph"})
public class GraphServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        EntityManagerFactory emf = null;
        EntityManager em = null;
        EntityTransaction transac = null;
        try {
                emf = Persistence.createEntityManagerFactory("leboncoin");
                em = emf.createEntityManager();
                transac = em.getTransaction();
                transac.begin();

                Query query = null;
                String option = (String) request.getAttribute("option");
                if (option!=null && !option.isEmpty()) {
                    if (option.equalsIgnoreCase("liste")) {
                        String jpql = "where searchItemBean.searchCategory=:1 ";
                        query = em.createQuery(jpql);
                        query.setParameter(1, "Ventes immobilières");
                    }
                } else {
                    String jpql = "where searchItemBean.searchText=:1 ";
                    query.setParameter(1, "156 GTA OR 147 gta");
                }
                
                JSONSerializer serializer = new JSONSerializer(); 
                List<ClassifiedBean> listClassified = query.getResultList();
                
//                List<GraphPointBean> resultList = new ArrayList<GraphPointBean>(list.size()); 
//                for (ClassifiedBean classifiedBean : list) {
//                    GraphPointBean graphPointBean = new GraphPointBean();
//                    graphPointBean.setY(classifiedBean.getPrice());
//                }
                
                out.write(serializer.serialize(listClassified));

                transac.commit();
        } catch (Exception e){
                System.err.println(e.getMessage());
                if (e.getCause() != null){
                        System.err.println(e.getCause().getMessage());
                }
                if (transac != null && transac.isActive()) transac.rollback();
        } finally {
                if (em != null) em.close();
                if (emf != null) emf.close();
                out.close();			
        }


    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>  
}
